﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Report;

public partial class admin_view_crm_old_leads : System.Web.UI.Page
{
    string PageCode = "GGRPTRADCRMAOL";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
            {
                CheckPageAccess();
            }

            ddlDay.Visible = true;
            ddlYear.Items.Clear();

            int _cryr = CE.Global.General.GetUTCDateTime().Year;
            int _styr = 2010;
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlYear.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }

            ddlDay.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
            pnlResult.Visible = false;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        pnlResult.Visible = false;
        LoadValues();
    }

    public bool ValidDate(Int32 Year, Int32 Month, Int32 Day)
    {
        bool _isValid = true;
        int daysInMonth = 0;
        string leap, monthValid, dayValid, yearValid;
        if (Year >= 0000)
        {
            yearValid = "yes";
        }
        else
        {
            yearValid = "no";
        }
        if (Year % 400 == 0)
        {
            leap = "yes";
        }
        else if (Year % 100 == 0)
        {
            leap = "no";
        }
        else if (Year % 4 == 0)
        {
            leap = "yes";
        }
        else
        {
            leap = "no";
        }

        switch (Month)
        {
            case 1: daysInMonth = 31;
                break;
            case 3: daysInMonth = 31;
                break;
            case 4: daysInMonth = 30;
                break;
            case 5: daysInMonth = 31;
                break;
            case 6: daysInMonth = 30;
                break;
            case 7: daysInMonth = 31;
                break;
            case 8: daysInMonth = 31;
                break;
            case 9: daysInMonth = 30;
                break;
            case 10: daysInMonth = 31;
                break;
            case 11: daysInMonth = 30;
                break;
            case 12: daysInMonth = 31;
                break;
        }

        if (Month >= 1 && Month <= 12)
        {
            monthValid = "yes";
        }
        else
        {
            monthValid = "no";
        }
        if (Month == 2)
        {
            if (leap == "no")
            {
                daysInMonth = 28;
            }
            else if (leap == "yes")
            {
                daysInMonth = 29;
            }
        }

        if (1 <= Day && Day <= daysInMonth)
        {
            dayValid = "yes";
        }

        else
        {
            dayValid = "no";
        }

        if ((dayValid == "yes") && (monthValid == "yes") && (yearValid == "yes"))
        {
            _isValid = true;
        }
        else
        {
            _isValid = false;
        }

        return _isValid;
    }

    private bool Validation()
    {
        bool _IsValid = false;
        if (ValidDate(Convert.ToInt32(ddlYear.Text), Convert.ToInt32(ddlMonth.Text), Convert.ToInt32(ddlDay.Text)))
        {
            _IsValid = true;
        }
        return _IsValid;
    }

    public string GetMonth(string month)
    {
        string result = "";
        if (month == "01")
        {
            result = " Jan ";
        }
        else if (month == "02")
        {
            result = " Feb ";
        }
        else if (month == "03")
        {
            result = " Mar ";
        }
        else if (month == "04")
        {
            result = " Apr ";
        }
        else if (month == "05")
        {
            result = " May ";
        }
        else if (month == "06")
        {
            result = " Jun ";
        }
        else if (month == "07")
        {
            result = " Jul ";
        }
        else if (month == "08")
        {
            result = " Aug ";
        }
        else if (month == "09")
        {
            result = " Sep ";
        }
        else if (month == "10")
        {
            result = " Oct ";
        }
        else if (month == "11")
        {
            result = " Nov ";
        }
        else if (month == "12")
        {
            result = " Dec ";
        }
        return result;
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
    }

    public void LoadValues()
    {
        if (Validation() == true)
        {
            Clear();
            DataSet _dsSummary = new DataSet();
            string ReportDate = ddlYear.Text + ddlMonth.Text + ddlDay.Text;


            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            SortedList _sPara = new SortedList();
            string _spName = "";

            _spName = "sprocCXTHReportOldLeadsCheckingbySelectedDate";
            _sPara.Add("@ReportDate", ReportDate);

            _dsSummary = _db.GetDataSet(_spName, _sPara);

            if (_dsSummary != null && _dsSummary.Tables.Count > 0 && _dsSummary.Tables[0].Rows.Count > 0)
            {
                pnlResult.Visible = true;
                if (_dsSummary.Tables[0].Rows[0]["OldLeadsCount"] != DBNull.Value && _dsSummary.Tables[0].Rows[0]["OldLeadsCount"].ToString() != string.Empty)
                {
                    lblTotalOldLeads.Text = _dsSummary.Tables[0].Rows[0]["OldLeadsCount"].ToString();
                }
                if (_dsSummary.Tables.Count > 1 && _dsSummary.Tables[1].Rows.Count > 0)
                {
                    string StartCommDate = "";
                    if (_dsSummary.Tables[1].Rows[0]["StartCommDate"] != DBNull.Value && _dsSummary.Tables[1].Rows[0]["StartCommDate"].ToString() != string.Empty)
                    {
                        StartCommDate = _dsSummary.Tables[1].Rows[0]["StartCommDate"].ToString();
                        lblStartCommDate.Text = StartCommDate.Substring(6, 2).ToString() + " " + GetMonth(StartCommDate.Substring(4, 2)).Trim() + " " + StartCommDate.Substring(0, 4).ToString();
                    }
                    string EndCommDate = "";
                    if (_dsSummary.Tables[1].Rows[0]["EndCommDate"] != DBNull.Value && _dsSummary.Tables[1].Rows[0]["EndCommDate"].ToString() != string.Empty)
                    {
                        EndCommDate = _dsSummary.Tables[1].Rows[0]["EndCommDate"].ToString();
                        lblEndCommDate.Text = EndCommDate.Substring(6, 2).ToString() + " " + GetMonth(EndCommDate.Substring(4, 2)).Trim() + " " + EndCommDate.Substring(0, 4).ToString();
                    }
                    string OldStartDate = "";
                    if (_dsSummary.Tables[1].Rows[0]["OldStartDate"] != DBNull.Value && _dsSummary.Tables[1].Rows[0]["OldStartDate"].ToString() != string.Empty)
                    {
                        OldStartDate = _dsSummary.Tables[1].Rows[0]["OldStartDate"].ToString();
                        lblOldStartDate.Text = OldStartDate.Substring(6, 2).ToString() + " " + GetMonth(OldStartDate.Substring(4, 2)).Trim() + " " + OldStartDate.Substring(0, 4).ToString();
                    }
                    string OldEndDate = "";
                    if (_dsSummary.Tables[1].Rows[0]["OldEndDate"] != DBNull.Value && _dsSummary.Tables[1].Rows[0]["OldEndDate"].ToString() != string.Empty)
                    {
                        OldEndDate = _dsSummary.Tables[1].Rows[0]["OldEndDate"].ToString();
                        lblOldEndDate.Text = OldEndDate.Substring(6, 2).ToString() + " " + GetMonth(OldEndDate.Substring(4, 2)).Trim() + " " + OldEndDate.Substring(0, 4).ToString();
                    }
                    string LeadStartDate = "";
                    if (_dsSummary.Tables[1].Rows[0]["LeadStartDate"] != DBNull.Value && _dsSummary.Tables[1].Rows[0]["LeadStartDate"].ToString() != string.Empty)
                    {
                        LeadStartDate = _dsSummary.Tables[1].Rows[0]["LeadStartDate"].ToString();
                        lblLeadStartDate.Text = LeadStartDate.Substring(6, 2).ToString() + " " + GetMonth(LeadStartDate.Substring(4, 2)).Trim() + " " + LeadStartDate.Substring(0, 4).ToString();
                    }
                    string LeadEndDate = "";
                    if (_dsSummary.Tables[1].Rows[0]["LeadEndDate"] != DBNull.Value && _dsSummary.Tables[1].Rows[0]["LeadEndDate"].ToString() != string.Empty)
                    {
                        LeadEndDate = _dsSummary.Tables[1].Rows[0]["LeadEndDate"].ToString();
                        lblLeadEndDate.Text = LeadEndDate.Substring(6, 2).ToString() + " " + GetMonth(LeadEndDate.Substring(4, 2)).Trim() + " " + LeadEndDate.Substring(0, 4).ToString();
                    } 
                }
            }
            else
            {
                Clear();
            }
        }
    }

    private void Clear()
    {
        lblStartCommDate.Text = "";
        lblEndCommDate.Text = "";
        lblOldStartDate.Text = "";
        lblOldEndDate.Text = "";
        lblLeadStartDate.Text = "";
        lblLeadEndDate.Text = "";
        lblTotalOldLeads.Text = "";
        pnlResult.Visible = false;
    }
}
